//
//  UserViewController.m
//  TourPlay
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "UserViewController.h"
#import "MBTwitterScroll.h"
#import "UserTableViewCell.h"
#import "RecordViewController.h"
#import "AboutViewController.h"
#import "StoreUpViewController.h"

@interface UserViewController () <UITableViewDataSource, UITableViewDelegate, MBTwitterScrollDelegate>

@property (nonatomic, retain) MBTwitterScroll *userMB;
@property (nonatomic, retain) UIImageView *headImageV;

@end

@implementation UserViewController
- (void)dealloc
{
    [_userMB release];
    [_headImageV release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"picholder@2x.png"] forBarMetrics:UIBarMetricsCompact];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = nil;
    
    // 状态栏
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.userMB = [[MBTwitterScroll alloc] initTableViewWithBackgound:[UIImage imageNamed:@"wode_morenbg@2x.jpg"]
                       avatarImage:[UIImage imageNamed:@"wode_morenbg@2x.jpg"]
                       titleString:@"我的"
                       subtitleString:@"这家伙很懒, 什么都没有留下"
                       buttonTitle:@"注销"];
    self.userMB.tableView.delegate = self;
    self.userMB.tableView.dataSource = self;
    self.userMB.delegate = self;
    self.userMB.tableView.bounces = NO;
    // 取消分割线
    self.userMB.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.userMB.tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.userMB];
    [_userMB release];
    
    
    [self addColorChangedBlock:^{
        
        self.userMB.titleLabel.normalTextColor = [ColorSingleton shareSingleton].cellTitleColor;
        self.userMB.titleLabel.nightTextColor = [ColorSingleton shareSingleton].cellTitleColor;
        
    }];

}
-(void)recievedMBTwitterScrollEvent
{
}
-(void)recievedMBTwitterScrollButtonClicked
{
    self.userMB.avatarImage.image = [UIImage imageNamed:@"picholder@2x.png"];
    self.userMB.titleLabel.text = @"geasggsag";
    [self.userMB.headerButton setTitle:@"登陆" forState:UIControlStateNormal];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.f;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section || 2 == section) {
        return 1;
    }
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";

//    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[UserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr] autorelease];
    }
    
    NSLog(@"+++%ld", cell.retainCount);
    
    [cell setValueWithIndexPath:indexPath];

    [self addColorChangedBlock:^() {
        cell.contentView.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        cell.contentView.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        
        cell.lineV.nightBackgroundColor = [ColorSingleton shareSingleton].cellLineColor;
        cell.lineV.normalBackgroundColor = [ColorSingleton shareSingleton].cellLineColor;
    }];

    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 选中时有置灰效果, 离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (1 == indexPath.section) {
        
        if (1 == indexPath.row) {
            RecordViewController *recordVC = [[RecordViewController alloc] init];
            
            recordVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:recordVC animated:YES];
            [recordVC release];
        } else if (2 == indexPath.row) {
            [self clearCaches];
        } else {
            StoreUpViewController *storeUpVC = [[StoreUpViewController alloc] init];
            
            storeUpVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:storeUpVC animated:YES];
            [storeUpVC release];
        }
            
    }
    
    if (2 == indexPath.section && 0 == indexPath.row) {
        AboutViewController *aboutVC = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:YES];
        [aboutVC release];
    }

}
#pragma mark - 清除缓存
- (void)clearCaches
{
    // 获取SDWebImage缓存图片大小
    NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"清除缓存"message:[NSString stringWithFormat:@"缓存大小为: %0.2f MB", cacheSize] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 清除缓存
        [[SDImageCache sharedImageCache] clearDisk];
        NSLog(@"缓存大小为:%0.2f MB", cacheSize);
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"缓存清除成功";
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertC addAction:action1];
    [alertC addAction:action2];
    [self presentViewController:alertC animated:YES completion:nil];
    
    



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
